var onlineUsersList = new Set();
function onlineUsers() {
    performQuery("user/online", "GET", function (data, textStatus, jqXHR) {
            if (data.status == 'ERROR') {
                alert('Error: ' + data.description);
            } else {
                var usersDiv = $('#onlineUsers');
                if (onlineUsersList.asArray().length == 0) {
                    usersDiv.empty();
                }
                var isOnline = [];
                var onlineArr = onlineUsersList.asArray();
                for (user in onlineArr) {
                    isOnline[onlineArr[user]] = false;
                }

                for (user in data.users) {
                    var userDto = data.users[user];
                    isOnline[userDto.username]  = true;
                    if (!onlineUsersList.contains(userDto.username)) {
                        onlineUsersList.add(userDto.username);
                        usersDiv.append("<div class='userLine'><div  title='" + userDto.username +
                            "' class='usersList user'>" + userDto.name +
                            "</div><a target='_blank' href='" + userDto.vkontakte + "'>" +
                            "<img src='/public/img/vk.gif'></a></div>");
                    }
                }

                for (user in isOnline) {
                    if (!isOnline[user]){
                        //alert(user);
                        onlineUsersList.remove(user);
                        usersDiv.find("div[title='"+user+"']").parent().remove();
                    }
                }
                updateBindings();
            }
        }
    );
}

function show(messages) {
    var messageContainer = $('#messages');
    messageContainer.empty();
    add(messages);
}
function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

function setCaretToPos(input, pos) {
    setSelectionRange(input, pos, pos);
}


function send() {
    if ($('#text').val().trim().length == 0) {
        return false;
    }
    $('#loader').css('display', 'block');
    $('#form').css('display', 'none');
    addMessage(getMessageText(), function () {
        $('#loader').css('display', 'none');
        $('#form').css('display', 'block');
        $('#text').val('');
        $('#quote').hide();
        $('#text').focus();
    });
    return false;
}
function updateBindings() {
    $('.user').unbind('click');
    $('.user').click(function () {
        var text = $('#text');
        text.val(text.val() + $(this).html() + ', ');
        setCaretToPos(document.getElementById("text"), text.val().length);
        return false;
    });
    $('.messagetext').unbind('click');
    $('.messagetext').click(function () {
        var quote = $('#quote');
        quote.find('.quotetext').html($(this).html());
        quote.find('.quoteauthor').html($(this).parent().find('.messageuser').html());
        quote.find('.quotedate').html($(this).parent().find('.messagedate').html());
        quote.show(500);
    });
}
